"""
组合概率公式【二项式系数计算阶乘统计量】
P(结果)=满足条件组合数 / 所有组合数
=>所有组合数=C(n, k) = n! / k!(n-k)! [从n个中选k个]
"""


class CombinedGroup:
    def __init__(self, total: int, target: int):
        self.total = total
        self.target = target
        self.group_cnt = self._calculate_total_combined_groups_cnt()

    def _calculate_total_combined_groups_cnt(self) -> int:
        """
        计算所有组合数

        :return:
        """
        if self.target > self.total:
            raise ValueError("Target number can not be greater than Total number！")
        # 分子
        numerator = self.total
        for i in range(1, self.total):
            numerator *= i
        # 分母
        denominator = 1
        for i in range(1, self.target + 1):
            denominator *= i
        diff = self.total - self.target
        for i in range(1, diff + 1):
            denominator *= i
        return numerator // denominator


"""
房间里有8人，分别佩戴着从1号到8号的纪念章，任选3人记录其纪念章号码，最大的号码为6的概率()
"""
# 计算总组合数
instance_total = CombinedGroup(8, 3)
# 3人中包含了6，还剩下两人，6是最大的情况下，剩下两人只能在1~5中出现，计算满足条件的组合数
instance_rest = CombinedGroup(5, 2)

r = instance_rest.group_cnt / instance_total.group_cnt
print(f"总组合数{instance_total.group_cnt}, 符合条件组合数{instance_rest.group_cnt}, 结果P={r:.06f}")
